
using System.Collections.Generic;
using Framework;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public partial class JoystickUI : UIWindowBase
{
    private ActorBase _actionActor;
    private Dictionary<int, GameObject> _pressBtn;
    private InputDevice InputDevice;
    protected override void OnAwake()
    {
        base.OnAwake();
        InputDevice = GameEntry.DeviceManager.AddDevice(-99);
        InputDevice.SetVirtual();
        this.input_VariableJoystick.OnStart = () =>
        {
            InputDevice.SetValue(GameInputName.Move, 0);
            InputDevice.Start(GameInputName.Move);
        };
        this.input_VariableJoystick.OnHold = (d) =>
        {
            InputDevice.SetValue(GameInputName.Move, d.x, d.y);
            InputDevice.Perform(GameInputName.Move);
        };
        this.input_VariableJoystick.OnEnd = () =>
        {
            InputDevice.Cancel(GameInputName.Move);
        };
        _pressBtn = new Dictionary<int, GameObject>();
        GameEntry.DeviceManager.AddInteraction<VirtualInteractionPress>("E").Config(InteractionType.PressAndRelease);
    }
    public override void SetParam(string name, object value)
    {
        base.SetParam(name, value);
        if (name == "updateActiveActor")
        {
            updateAction();
        }
    }
    private void updateAction()
    {
        var actor = GameEntry.Context.World.ActiveActor;
        if (_actionActor == actor) return;
        _actionActor = actor;
        Utils.Unity.SetActive(this.action1_Button, false);
        Utils.Unity.SetActive(this.action2_Button, false);
        Utils.Unity.SetActive(this.action3_Button, false);
        if (_actionActor == null)
        {
            return;
        }
        var menuId = ((int)_actionActor.GetActorType());
        var tpl = GameEntry.Table.ActionMenu.Get(menuId);
        if (tpl == null) return;
        Utils.Unity.SetActive(this.icon1_Image, false);
        GameEntry.Atlas.SetSprite(this.icon1_Image, tpl.Icon, false, true);
        Utils.Unity.SetText(this.name1_Text, tpl.Name);
        Utils.Unity.SetActive(this.action1_Button, true);
    }
    protected override void OnOpen()
    {
        base.OnOpen();
        Utils.Unity.SetActive(this.action1_Button, false);
        Utils.Unity.SetActive(this.action2_Button, false);
        Utils.Unity.SetActive(this.action3_Button, false);
    }
    protected override void OnButtonClick(Button target)
    {
        base.OnButtonClick(target);
        if (target == this.action1_Button)
        {
            InputDevice.SetValue(GameInputName.Action_A, 1);
            InputDevice.Start(GameInputName.Action_A);
            InputDevice.Cancel(GameInputName.Action_A);
        }
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        var target = eventData.pointerEnter;
        _pressBtn.Add(eventData.pointerId, target);
        if (target == this.action1_Button.gameObject)
        {
            InputDevice.SetValue(GameInputName.Action_A, 1);
            InputDevice.Start(GameInputName.Action_A);
        }
        else if (target == this.actionE_Button.gameObject)
        {
            InputDevice.SetValue("E", 1);
            InputDevice.Start("E");
        }
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        if (_pressBtn.TryGetValue(eventData.pointerId, out GameObject target))
        {
            _pressBtn.Remove(eventData.pointerId);
            if (target == this.action1_Button.gameObject)
            {
                InputDevice.Cancel(GameInputName.Action_A);
            }
            else if (target == this.actionE_Button.gameObject)
            {
                InputDevice.Cancel("E");
            }
        }
    }
}